//var compnent_list = new Array("userloginid", "userPawd", "u_v_i_c", "postButton");
var basePath = "";
var returnURL = "";
var compnent_list = new Array("userloginid", "userPawd", "postButton");
var prevLoadImg = new Image();
prevLoadImg.src="../resources/images/loading.gif";
var waitingSpan = '<span id="postButtonWait"><img src="resources/images/loading.gif" />&nbsp;请稍候，处理中...... &nbsp;</span>';

$(document).ready(function(){
    $("#username").focus();
    //记住我，自动登录
	var userloginid = getCookieValue("UHsTwUjda12jLk39");
	if(userloginid!=null && userloginid!=''){
		$("#username").val(userloginid);
        $("#pwd").val(userloginid);//not empty
        ajaxLogin();//auto login
	}
});

function ajaxLogin(basePath){
	debugger;
	var checked = checkData();
	if(checked){
		//$('#msg_id').html("");
		//var button_td = $("#button_td").html();
		//$('#button_td').html(waitingSpan);
        if(returnURL==null || returnURL=="undefined"){
            returnURL = "";//如果没有传入此参数，就赋予空字符串
        }
    alert("returnURL==="+returnURL);
        var userloginid=document.getElementById("username").value;
		var userpwd=document.getElementById("pwd").value;
        var remUser = $("#remUser").val();
		$.post(basePath+'/ajaxLogin?act=login', {yonghu: userloginid, mima: userpwd, remUser:remUser}, function(data) {
			var jsonarray = eval("("+data+")");
			var obj = jsonarray;
	    	if(obj.msg=='ok'){//成功登录
	    		if($("#sfjzdlzt[checked]")[0]){//记住我
	    			var exp = new Date(); 
	    			exp.setTime(exp.getTime() + 10*365*24*60*60*1000);//10年
	    			document.cookie = "userLoginId="+userLoginId+";path=/;expires="+exp.toGMTString();
	    			document.cookie = "userPawd="+userPawd+";path=/;expires="+exp.toGMTString();
	    		}
	    		if(obj.usertype == '0'){
					debugger;
	    			   window.location.href = basePath+'/identity/index.action';
	    		}else{
	    			if(returnURL==""){//判断上一个url地址是否为空  如果没有上一个url地址 直接到首页
	    				window.location.href =basePath+"/index.jhtm";//返回首页
	    			}else{//如果有上一个url地址，则跳转到上一个url地址
	    			    window.location.href = returnURL;
	    			}
	    			
	    		}
		    }else{
		    	if(obj.msg=="already" || obj.msg=="本地已有其他账户登录，如需切换用户请先退出登陆！"){
		    		alert("本地已有其他账户登录，如需切换用户请先退出登陆！");
			    	   window.location.href =basePath+"/index.jhtm";
		    	}else if(obj.msg=="该帐号未激活，请登录注册邮箱点击激活链接！" ){
		    		 var submit = function (v, h, f) {
		   			    if (v == 'ok')
		   			       window.location.href= basePath+"/s/regist/resendEmail?username="+userloginid;
		   			    else if (v == 'cancel')
		   			    return true; //close
		   			};
		   			$.jBox.confirm("您尚未激活账户，重新发送激活邮件？", "提示", submit);
		   			return;
		    	}else{
		    		alert(obj.msg);
		    	}
			}
		});
	}
}

function checkData(){
	var userloginid=document.getElementById("username").value;
	var userpwd=document.getElementById("pwd").value;
    if (userloginid == "") {
        alert("请输入用户登录账号！");
      //  document.getElementById("userloginid").focus();
        return false;
    }
    /*
    if (isContainChinese(userloginid)) {
        alert("用户登录账号不接受中文！");
        document.getElementById("userloginid").focus();
        return false;
    }
    */
    //if( getLength(userloginid)>50 ){
	//  alert("用户登录账号长度不能超过50！");
	 // document.getElementById("userloginid").focus();
	 // return false;
   // }
    if (userpwd == "") {
        alert("请输入用户登录密码！");
      //  document.getElementById("userpwd").focus();
        return false;
    }
  //  if( getLength(userpwd)>50 ){
	//  alert("用户登录密码长度不能超过50！");
	 // document.getElementById("userpwd").focus();
	//  return false;
   // }
    return true;
}
//转到注册页面
function openReg(){
	top.$(".colorboxReg").click();
}
